/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import com.velocityme.enums.LoginStatus;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQueries( {
    @NamedQuery(name="findAllLogins",
                query="SELECT l FROM Login l")/*,
    @NamedQuery(name="findAllLoggedOn",
                query="SELECT l FROM Login l WHERE l.loginStatus = com.velocityme.enum.LoginStatus.LOGGED_ON")*/
})
public class Login implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date loginTime;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date lastActionTime;

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date logoutTime;

    private String remoteHost;

    private LoginStatus loginStatus;

    @ManyToOne
    private User user;

    @ManyToOne
    private EncodedLicence licenceKey;

    public Login() {

    }

    public Login(User user, EncodedLicence licenceKey, String remoteHost) {
        this.user = user;
        this.licenceKey = licenceKey;
        this.remoteHost = remoteHost;
        loginTime = new Date();
        lastActionTime = new Date();
        logoutTime = null;
        loginStatus = LoginStatus.LOGGED_ON;
        user.getLogins().add(this);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getLastActionTime() {
        return lastActionTime;
    }

    public void setLastActionTime(Date lastActionTime) {
        this.lastActionTime = lastActionTime;
    }

    public LoginStatus getLoginStatus() {
        return loginStatus;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLicenceKey(EncodedLicence licenceKey) {
        this.licenceKey = licenceKey;
    }

    public void setLogoutTime(Date logoutTime) {
        this.logoutTime = logoutTime;
    }

    public void setLoginStatus(LoginStatus loginStatus) {
        this.loginStatus = loginStatus;
    }

    public Date getLogoutTime() {
        return logoutTime;
    }

    public User getUser() {
        return user;
    }

    public boolean isLoggedOn() {
        return loginStatus == LoginStatus.LOGGED_ON;
    }

    public String getRemoteHost() {
        return remoteHost;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Login)) {
            return false;
        }
        Login other = (Login) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.velocityme.entity.Login[id=" + id + "]";
    }

}
